package entity;

import java.math.BigDecimal;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import util.HibernateUtil;


public class TestUser {
	@Test
	public void testSave(){
		Session s = HibernateUtil.getSession();
		Transaction t =s.beginTransaction();
		User heantai= new User();
		heantai.setName("heantai");
		heantai.setScore(100);
		heantai.setVerify(true);
		heantai.setSalary(new BigDecimal("1000.55"));
		heantai.setHireDate(new Date());
		Integer id=(Integer) s.save(heantai);//save专指insert,返回生成的ID
		System.out.println(id);
		t.commit();//提交后自动关闭了session(此session使用的是getCurrentSession获得)
		
	}
	
	@Test
	public void testUpdate(){
		Session s = HibernateUtil.getSession();
		Transaction t=s.beginTransaction();
		User user=(User) s.get(User.class, 2);
		user.setName("zhouyu");
		t.commit();
	}
	//查询用户——通过Id   get
	@Test
	public void testGet(){
		Session s = HibernateUtil.getSession();
		s.beginTransaction();
		User user=(User) s.get(User.class, 18);
		System.out.println(user);
	}
	//查询用户——通过Id    load
	@Test
	public void testLoad(){
		Session s = HibernateUtil.getSession();
		s.beginTransaction();
		User user=(User) s.load(User.class, 7);
		System.out.println(user.getName());//此句执行与不执行表明，load时并未查数据库，用到时才查
	}
	
	@Test
	public void testDelete(){
		//1、获取session
		Session session = HibernateUtil.getSession();
		Transaction t = session.beginTransaction();
		
		//2、获取实体对象（User）
		User user = (User)session.load(User.class, 2);

		//3、删除实体
		session.delete(user);
		
		t.commit();
		
	}
	
}
